﻿using System;

namespace Framework.Event
{
    /// <summary>
    /// 事件管理类
    /// </summary>
    public class EventManager : IEventManager, IModule
    {
        private EventPool<IEventArg> m_EventPool = null;

        public int EventCount
        {
            get
            {
                if (m_EventPool == null) return 0;
                return m_EventPool.EventCount;
            }
        }
        public void Subscriber(int eventId, EventHandler<IEventArg> handler)
        {
            if (m_EventPool == null) return;
            m_EventPool.Subscriber(eventId, handler);
        }
        public void UnSubscriber(int eventId, EventHandler<IEventArg> handler)
        {
            if (m_EventPool == null) return;
            m_EventPool.UnSubscriber(eventId, handler);
        }
        public void Fire(object sender, IEventArg arg)
        {
            if (m_EventPool == null) return;
            m_EventPool.Fire(sender, arg);
        }
        public void FireNow(object sender, IEventArg arg)
        {
            if (m_EventPool == null) return;
            m_EventPool.FireNow(sender, arg);
        }
        public int Priority
        {
            get
            {
                return ModulePriority.EventPoolPriority;
            }
        }
        public void Init()
        {
            m_EventPool = new EventPool<IEventArg>();
        }
        public void Update(float frameTime, float realTime)
        {
            if(m_EventPool != null)
                m_EventPool.Update(frameTime, realTime);
        }
        public void Reset()
        {
            if (m_EventPool != null)
                m_EventPool.Destroy();
        }
        public void Destroy()
        {
            if (m_EventPool == null) return;
            m_EventPool.Destroy();
            m_EventPool = null;
        }
    }
}
